package com.cdd.role.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cdd.role.mapper.RoleMapper;
import com.cdd.role.openFeign.RolePermissionFeign;
import com.cdd.role.service.RoleService;
import com.cdd.role.vo.Pages;
import com.common.entity.Permission;
import com.common.entity.Role;
import com.common.untils.CommonResult;
import com.common.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * TODO
 *
 * @author lenovo
 * @version 1.0
 * @since 2022-09-01  21:17:06
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RolePermissionFeign rolePermissionFeign;
    //查询全部
    @Override
    public CommonResult queryAll(Integer currentPage,Integer pageSize,Role role) {
        Page<Role> page=new Page<>(currentPage,pageSize);
        QueryWrapper<Role> wrapper=new QueryWrapper<Role>();
        wrapper.eq("is_deleted",0);
        //判断有没有RoleName有就查询,没有就跳过
        if (StringUtils.hasText(role.getRoleName())){
            wrapper.like("role_name",role.getRoleName());
        }
//        if (StringUtils.isEmpty(pages.getState())){
//            wrapper.eq("state",pages.getState());
//        }
        if (role.getState()!=null){
            wrapper.eq("state",role.getState());
        }
        Page<Role> list = roleMapper.selectPage(page,wrapper);
         return new CommonResult(2000,"查询成功",list);

    }
    //添加角色
    @Override
    public CommonResult insetRole(Role role) {
        Role role1=new Role();
        role1.setRoleName(role.getRoleName());
        role1.setRemark(role.getRemark());
        role1.setIsDeleted(0);
        role1.setState(0);
        int insert = roleMapper.insert(role1);
        if (insert==1) {
            return new CommonResult(2000,"添加成功",null);
        }else {
            return new CommonResult(5000,"添加失败",null);
        }
    }
    //修改角色
    @Override
    public CommonResult update(Role role) {
        QueryWrapper wrapper=new QueryWrapper();
        Role role1=new Role();
        role1.setRoleName(role.getRoleName());
        role1.setRemark(role.getRemark());
        wrapper.eq("is_deleted",0);
        wrapper.eq("role_id",role.getRoleId());
        int update = roleMapper.update(role1, wrapper);
        if (update == 1) {
            return new CommonResult(2000,"修改成功",null);
        }else {
            return new CommonResult(5000,"修改失败",null);
        }
    }
    //删除角色
    @Override
    public CommonResult deleted(String roleId) {
        QueryWrapper<Role> wrapper=new QueryWrapper();
        wrapper.eq("role_id",roleId);
        int delete = roleMapper.delete(wrapper);
        if (delete==1){
            return new CommonResult(2000,"删除成功",null);
        }else {
            return new CommonResult(5000,"删除失败",null);
        }
    }
    //修改状态
    @Override
    public CommonResult updateState(String roleId, Integer state) {
        QueryWrapper<Role> wrapper=new QueryWrapper<>();
        wrapper.eq("role_id",roleId);
        Role role=new Role();
        if (state==0) {
            role.setState(0);
        }
        if (state==1) {
            role.setState(1);
        }
        int updateState = roleMapper.update(role, wrapper);
        if (updateState == 1) {
            return new CommonResult(2000,"状态修改成功",null);
        }else {
            return new CommonResult(5000,"状态修改失败",null);
        }
    }
    //根据角色id查询当前角色具有的权限和所有权限
    @Override
    public CommonResult selectPermission(String roleId) {
        //所有的权限
        List<Permission> permissionList = rolePermissionFeign.selectAllPermission();
        System.out.println(permissionList+"-----");
        //设置层级关系
        //一级菜单
        List<Permission> firstMenus = new ArrayList<>();
        for (Permission first : permissionList) {
            if (first.getPid().equals("0")) {
                firstMenus.add(first);
            }
        }
        //为一级菜单设置二级菜单
        for (Permission first : firstMenus) {
            //根据一级菜单id 查询该菜单的二级菜单
            first.setChildren(findChildren(permissionList, first.getId()));
        }
        //根据角色id查询当前角色具有的权限
        List<Permission> permission = roleMapper.selectPermissionByRoleId(roleId);
        Map map=new HashMap();
        map.put("firstMenus",firstMenus);
        map.put("permission",permission);
        return new CommonResult(2000,"查询查询成功",map);
    }

    @Override
    public List<Role> ApiSelectRoleALl() {
        QueryWrapper<Role> wrapper=new QueryWrapper();
        wrapper.eq("is_deleted",0);
        List<Role> list = roleMapper.selectList(wrapper);
        return list;
    }
    //分页查询
    @Override
    public CommonResult selectAllRoleName(Integer roleCurrentPage, Integer rolePageSize, Role role) {
        Page<Role> page=new Page<>(roleCurrentPage,rolePageSize);
        QueryWrapper<Role> wrapper=new QueryWrapper();
        wrapper.eq("is_deleted",0);
        if (StringUtils.hasText(role.getRoleName())){
            wrapper.like("role_name",role.getRoleName());
        }
        Page page1 = roleMapper.selectPage(page, wrapper);

       return new CommonResult(2000,"分页查询成功",page1);
    }

    //方法递归
    private List<Permission> findChildren (List < Permission > permissionList, String id){
        List<Permission> children = new ArrayList<>();
        for (Permission p : permissionList) {
            if (p.getPid().equals(id)) {
                children.add(p);
            }
        }
        for (Permission child : children) {
            child.setChildren(findChildren(permissionList, child.getId()));
        }
        return children;

    }

}
